package com.zito.health.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.zito.health.common.service.impl.BaseServiceImpl;
import com.zito.health.entity.Laboratory;
import com.zito.health.entity.LaboratoryInspect;
import com.zito.health.mapper.LaboratoryInspectImgMapper;
import com.zito.health.mapper.LaboratoryInspectMapper;
import com.zito.health.service.LaboratoryInspectService;
import com.zito.health.service.LaboratoryService;
import com.zito.health.vo.LaboratoryInspectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Array;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 巡检
 * @auth
 */
@Slf4j
@Service
public class LaboratoryInspectServiceImpl extends BaseServiceImpl<LaboratoryInspect> implements LaboratoryInspectService {
    @Autowired
    private LaboratoryInspectMapper laboratoryInspectMapper;
    @Autowired
    private LaboratoryInspectImgMapper laboratoryInspectImgMapper;
    @Autowired
    private LaboratoryService laboratoryService;

    // 添加巡检表和巡检图片表
    @Transactional
    @Override
    public void saveInspect(LaboratoryInspect laboratoryInspect) {
        // 查询实验室信息 byid
        Laboratory laboratory = laboratoryService.queryById(laboratoryInspect.getLaboratoryId());
        // 从实验室取值区域和单位
        laboratoryInspect.setAreaId(laboratory.getAreaId());
        laboratoryInspect.setUnitId(laboratory.getUnitId());
        // 巡检时间
        laboratoryInspect.setTime(new Date());
        // 调用新增巡检
        laboratoryInspectMapper.saveInspect(laboratoryInspect);
        String[] split = laboratoryInspect.getUrl().split(",");
        for (int i = 0; i < split.length; i++) {
            // 调用新增巡检图片
            laboratoryInspectImgMapper.addInspectImg(laboratoryInspect.getId(),split[i]);
        }
    }

    /**
     * 查询详情
     * @param id
     * @return
     * @auth sun
     */
    @Override
    public LaboratoryInspectVo queryInspectByid(Integer id) {
        return laboratoryInspectMapper.queryInspectByid(id);
    }

    /**
     * 删除巡检和删除图片
     */
    @Override
    @Transactional
    public void deleteInspect(Integer id) {
        super.delete(id); // 删除巡检
        laboratoryInspectImgMapper.deleteInspectImg(id);// 删除巡检图片
    }

    /**
     * 查询列表
     * @return
     * @auth sun
     */
    @Override
    public List<LaboratoryInspectVo> queryList(LaboratoryInspect laboratoryInspect) {
        return laboratoryInspectMapper.queryList(laboratoryInspect);
    }

}
